package com.example.Horarios;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Fragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.example.myfirstapp.R;

public class CargarHorariosFragment extends Fragment {
	
	public CargarHorariosFragment(){
	
	}
	
	EditText etGanancia;
	TextView tvFechaInicio,tvFechaFin;
	View v;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_cargar_horarios, container,
				false);
		v = rootView;
		
		cargarControles();
		return rootView;
	}
	
	protected void cargarControles(){
		LinearLayout inicio = (LinearLayout) v.findViewById(R.id.inicio);
		inicio.setOnClickListener(new OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	fechaPopUpInicio();
	        }
	    });
		
		LinearLayout fin = (LinearLayout) v.findViewById(R.id.fin);
		fin.setOnClickListener(new OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	fechaPopUpFin();
	        }
	    });
		
		//Setear horario actual al cargar
		final Calendar c = Calendar.getInstance();
		mYearF = mYearI = c.get(Calendar.YEAR);
		mMonthF = mMonthI = c.get(Calendar.MONTH);
		mDayF = mDayI = c.get(Calendar.DAY_OF_MONTH);
		
		mHourF = mHourI = c.get(Calendar.HOUR_OF_DAY);
		mMinuteF = mMinuteI = c.get(Calendar.MINUTE);
		
		TextView tvFechaI = (TextView) v.findViewById(R.id.tvFechaInicio);
		TextView tvFechaF = (TextView) v.findViewById(R.id.tvFechaFin);
		
		String horario = ((mDayI < 10) ? "0" + mDayI : mDayI ) + "/" + (((mMonthI + 1) < 10) ? "0" + (mMonthI + 1) : (mMonthI + 1) )  + "/" + mYearI ;
    	horario = horario + " " + ((mHourI < 10) ? "0" + mHourI : mHourI )  + ":" + ((mMinuteI < 10) ? "0" + mMinuteI : mMinuteI ); 
    	
    	tvFechaI.setText(horario);
    	tvFechaF.setText(horario);    	
	}
	
	
	//Pop up con fecha hora
	int mYearI,mMonthI,mDayI,mHourI,mMinuteI;
	int mYearF,mMonthF,mDayF,mHourF,mMinuteF;
	
	protected void fechaPopUpInicio(){
		DatePickerDialog dpd = new DatePickerDialog(v.getContext(),
		        new DatePickerDialog.OnDateSetListener() {
		 
		            @Override
		            public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
		            	 if (view.isShown()) {
		            	TextView tvFecha = (TextView) v.findViewById(R.id.tvFechaInicio);
		            	tvFecha.setText(((dayOfMonth < 10) ? "0" + dayOfMonth : dayOfMonth ) + "/" + (((monthOfYear + 1) < 10) ? "0" + (monthOfYear + 1) : (monthOfYear + 1) )  + "/" + year);
		            	
		            	mYearI = year;
		            	mMonthI = monthOfYear;
		            	mDayI = dayOfMonth;
		            	
		            	horaPopUpInicio();
		            	 }
		            }
		        }, mYearI, mMonthI, mDayI);
		dpd.show();
	}
	
	protected void horaPopUpInicio(){
		TimePickerDialog tpd = new TimePickerDialog(v.getContext(),
		        new TimePickerDialog.OnTimeSetListener() {
		 
		            @Override
		            public void onTimeSet(TimePicker view, int hourOfDay,
		                    int minute) {
		            	if (view.isShown()) {
		            	TextView tvFecha = (TextView) v.findViewById(R.id.tvFechaInicio);
		            	tvFecha.setText(tvFecha.getText() + " " + ((hourOfDay < 10) ? "0" + hourOfDay : hourOfDay )  + ":" + ((minute < 10) ? "0" + minute : minute ) );
		            	mHourI = hourOfDay;
		            	mMinuteI = minute;
		            	}
		            }
		        }, mHourI, mMinuteI, true);
		tpd.show();
	}
	
	protected void fechaPopUpFin(){	 	
		DatePickerDialog dpd = new DatePickerDialog(v.getContext(),
		        new DatePickerDialog.OnDateSetListener() {
		 
		            @Override
		            public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
		            	 if (view.isShown()) {
		            	TextView tvFecha = (TextView) v.findViewById(R.id.tvFechaFin);
		            	tvFecha.setText(((dayOfMonth < 10) ? "0" + dayOfMonth : dayOfMonth ) + "/" + (((monthOfYear + 1) < 10) ? "0" + (monthOfYear + 1) : (monthOfYear + 1) )  + "/" + year);
		            	
		            	mYearF = year;
		            	mMonthF = monthOfYear;
		            	mDayF = dayOfMonth;
		            	
		            	horaPopUpFin();
		            	 }
		            }
		        }, mYearF, mMonthF, mDayF);
		dpd.show();
	}
	
	protected void horaPopUpFin(){
		TimePickerDialog tpd = new TimePickerDialog(v.getContext(),
		        new TimePickerDialog.OnTimeSetListener() {
		 
		            @Override
		            public void onTimeSet(TimePicker view, int hourOfDay,
		                    int minute) {
		            	if (view.isShown()) {
		            	TextView tvFecha = (TextView) v.findViewById(R.id.tvFechaFin);
		            	tvFecha.setText(tvFecha.getText() + " " + ((hourOfDay < 10) ? "0" + hourOfDay : hourOfDay )  + ":" + ((minute < 10) ? "0" + minute : minute ) );
		            	mHourI = hourOfDay;
		            	mMinuteI = minute;
		            	}
		            }
		        }, mHourF, mMinuteF, true);
		tpd.show();
	}
		
}
